const path = require("path");
const htmlWebpackPlugin = require("html-webpack-plugin");
const miniCssExtractPlugin = require("mini-css-extract-plugin");

const textWebpackPlugin = require("./myPlugins/text-webpack-plugin.js");

const Purifycss = require("purifycss-webpack");
const glob = require("glob-all");

const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "./dist"),
    filename: "[name]-[hash:6].js",
  },
  mode: "development",
  resolveLoader: {
    modules: ["./node_modules", "./myLoaders"],
  },

  resolve: {
    alias: {
      //起别名
      "@img": path.resolve(__dirname, "./src/images"),
    },
    //导入模块后缀列表
    //推荐所有的模块都添加上后缀
    extensions: [".js", ".json"],
  },
  devtool: "inline-source-map",
  module: {
    rules: [
      {
        test: /\.less$/,
        include: path.resolve(__dirname, "./src/css"),
        use: [
          // {
          //   loader: miniCssExtractPlugin.loader,
          //   options: {
          //     publicPath: "../",
          //   },
          // },
          "style-loader",
          "css-loader",
          "postcss-loader",
          "less-loader",
        ],
      },
      {
        test: /\.css$/,
        // exclude 排除
        include: path.resolve(__dirname, "./src"),
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
      {
        test: /\.(png|jpe?g|gif)$/,
        include: path.resolve(__dirname, "./src/images"),
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name]-[hash:6].[ext]",
              outputPath: "images/",
            },
          },
          // "image-webpack-loader",
        ],
      },
      {
        test: /\.js$/,
        include: path.resolve(__dirname, "./src"),
        use: "babel-loader",
      },
    ],
  },
  devServer: {
    contentBase: "./dist",
    port: 8081,
    open: true,
    hot: true,
    proxy: {
      "/api": {
        target: "http://localhost:9092",
      },
    },
    //不自动刷新浏览器窗口
    hotOnly: true,
  },
  optimization: {
    usedExports: true,
  },
  plugins: [
    new htmlWebpackPlugin({
      template: "./src/index.html",
      filename: "index.html",
    }),

    new Purifycss({
      //摇树的范围 引入css的js模块  引入css的html
      paths: glob.sync([
        path.resolve(__dirname, "./dist/*.html"),
        path.resolve(__dirname, "./src/*.js"),
      ]),
    }),
    // new textWebpackPlugin({
    //   name: "my plugin",
    // }),
    new miniCssExtractPlugin({
      filename: "css/[name]-[contenthash:6].css",
    }),
    // new webpack.HotModuleReplacementPlugin(),
  ],
};
